from django.contrib import admin

# Register your models here.
from django.contrib import admin
from .models import AttendanceRecord


@admin.register(AttendanceRecord)
class AttendanceRecordAdmin(admin.ModelAdmin):
    """考勤记录的Admin配置"""
    list_display = ('id', 'employee', 'record_date', 'check_in_time',
                    'check_out_time', 'status', 'late_minutes', 'is_corrected')
    list_filter = ('status', 'is_corrected', 'record_date')
    search_fields = ('employee__name', 'employee__work_id', 'correction_note')
    date_hierarchy = 'record_date'
    ordering = ('-record_date', 'employee__name')
    list_per_page = 30

    # 详情页字段布局
    fieldsets = (
        ('员工信息', {
            'fields': ('employee', 'record_date')
        }),
        ('打卡时间', {
            'fields': ('check_in_time', 'check_out_time')
        }),
        ('考勤状态', {
            'fields': ('status', 'late_minutes', 'early_leave_minutes')
        }),
        ('补卡信息', {
            'fields': ('is_corrected', 'correction_note')
        }),
        ('系统信息', {
            'fields': ('status_updated_at', 'created_at', 'updated_at'),
            'classes': ('collapse',)
        }),
    )

    # 只读字段
    readonly_fields = ('status_updated_at', 'created_at', 'updated_at')

    def save_model(self, request, obj, form, change):
        # 保存时重新计算考勤状态
        obj.calculate_status()
        super().save_model(request, obj, form, change)
